import { ref, reactive, onMounted } from 'vue';
import { getRdsUrl, rdsRequestAsync } from './rdsServices';

export function useBaseType() {
    const baseData = ref<any[]>([]);
    const baseTypeItems = ref<{
        value: string;
        title: string;
    }[]>([]);
    const loaded = ref<boolean>(false);

    const rdsUrl = getRdsUrl();

    const initMode = async () => {
        const result = await rdsRequestAsync(rdsUrl.bcApi, '合同基数类型定义', 'FetchBaseType', '');
        if (!result.HasException) {
            baseData.value = result.ObjectData;
            baseTypeItems.value = result.ObjectData.map(item => ({
                value: item.c_base_type,
                title: `[${item.c_base_type}]${item.c_name}`,
            }));
        }
    }

    const getBaseType = async () => {
        const result = await await rdsRequestAsync(rdsUrl.bcApi, '合同基数类型定义', 'FetchBaseType', '');
        if (!result.HasException) {
            return result.ObjectData;
        }
        return [];
    }

    const fetchBaseTypeItems = async (base_kind?: string[], contype?: string) => {

        const data = await getBaseType()
        const items = data
            .filter(b => b.c_is_fee != '0')
            .filter(b => (base_kind === undefined || base_kind.includes(b.c_base_kind)) && b.c_status == '1')
            .map(b => ({ label: `[${b.c_base_type}]${b.c_name}`, title: `[${b.c_base_type}]${b.c_name}`, value: b.c_base_type }))
        return items
    }

    const BaseTypes = ref<any>({});

    const getBaseTypeItems = (base_kind?: string[], contype?: string) => {

        const items = baseData.value
            .filter(b => b.c_is_fee != '0')
            .filter(b => (base_kind === undefined || base_kind.includes(b.c_base_kind)) && b.c_status == '1')
            .map(b => ({ label: `[${b.c_base_type}]${b.c_name}`, title: `[${b.c_base_type}]${b.c_name}`, value: b.c_base_type }))

        return items
    }

    const getBaseTypeName = (base_type?: string[]) => {

        const name = baseData.value
            .find(b => b.c_base_type == base_type)?.c_name

        return name
    }


    const getBotBaseType = (base_kind?: string[]) => {

        const items = baseData.value
            .filter(b => b.c_is_bot != '0')
            .filter(b => (base_kind || []).includes(b.c_base_kind) && b.c_status == '1')
            .map(b => ({ label: `[${b.c_base_type}]${b.c_name}`, title: `[${b.c_base_type}]${b.c_name}`, value: b.c_base_type }))
        return items
    }


    const getBaseTypeOptions = (base_kind?: string[]) => {

        const items = baseData.value
            .filter(b => (base_kind || []).includes(b.c_base_kind) && b.c_status == '1')
            .map(b => ({ label: `[${b.c_base_type}]${b.c_name}`, value: b.c_base_type }))
        return items
    }

    const getBaseKind = (base_type?: string[]) => {

        const items = baseData.value.find(b => b.c_base_type == base_type)?.c_base_kind || ''
        return items
    }

    const refresh = () => {
        initMode();
    }

    onMounted(() => {
        if (!loaded.value) {
            loaded.value = true;
            initMode();
        }
     });

    return {
        baseTypeItems: baseTypeItems,
        baseTypes: baseData,
        getBaseTypeItems,
        getBotBaseType,
        getBaseTypeOptions,
        getBaseKind,
        fetchBaseTypeItems,
        refresh,
        getBaseTypeName
    };
}
